<template>
  <el-row>
    <el-col :span="12">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="交易模式" prop="tradeMode">
          <el-select
            v-model="form.tradeMode"
            placeholder="请选择交易模式"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.trading_mode"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单笔限额" prop="singleTransactionLimit">
          <el-input v-model="form.singleTransactionLimit" placeholder="请输入单笔限额" />
        </el-form-item>
        <el-form-item label="单日限额" prop="dailyTransactionLimit">
          <el-input v-model="form.dailyTransactionLimit" placeholder="请输入单日限额" />
        </el-form-item>
        <el-form-item label="单用户最大欠赛笔数" prop="maxArrearsCount">
          <el-input
            v-model="form.maxArrearsCount"
            placeholder="请输入单用户最大欠赛笔数"
          />
        </el-form-item>
        <el-form-item label="单人最大离线笔数" prop="maxOfflineCount">
          <el-input v-model="form.maxOfflineCount" placeholder="请输入单人最大离线笔数" />
        </el-form-item>
        <el-form-item label="扣费顺序" prop="deductionOrder">
          <el-select
            v-model="form.deductionOrder"
            placeholder="请选择扣费顺序"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.charging_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单笔充值上限" prop="singleRechargeLimit">
          <el-input v-model="form.singleRechargeLimit" placeholder="请输入单笔充值上限" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="submit">保存</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
import { getTradeSettings, updateSetting } from "@/api/campus/setting";
export default {
  dicts: ["charging_type", "trading_mode"],
  data() {
    return {
      form: {
        tradeMode: null,
        singleTransactionLimit: null,
        dailyTransactionLimit: null,
        maxArrearsCount: null,
        maxOfflineCount: null,
        deductionOrder: null,
      },
      // 表单校验
      rules: {
        tradeMode: [{ required: true, message: "交易模式不能为空", trigger: "change" }],
        singleTransactionLimit: [
          { required: true, message: "单笔限额不能为空", trigger: "blur" },
        ],
        dailyTransactionLimit: [
          { required: true, message: "单日限额不能为空", trigger: "blur" },
        ],
        maxArrearsCount: [
          { required: true, message: "单用户最大欠赛笔数不能为空", trigger: "blur" },
        ],
        maxOfflineCount: [
          { required: true, message: "单人最大离线笔数不能为空", trigger: "blur" },
        ],
        deductionOrder: [
          { required: true, message: "扣费顺序不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getTradeSettingsFun();
  },
  methods: {
    getTradeSettingsFun() {
      var that = this;
      getTradeSettings().then((res) => {
        if (res.code == 200) {
          if (res.data) {
            that.form = res.data;
          } else {
            that.form = {
              tradeMode: null,
              singleTransactionLimit: null,
              dailyTransactionLimit: null,
              maxArrearsCount: null,
              maxOfflineCount: null,
              deductionOrder: null,
            };
          }
        } else {
        }
      });
    },
    submit() {
      /** 提交按钮 */
      this.$refs["form"].validate((valid) => {
        if (this.form.id != null) {
          updateSetting(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
          });
        } else {
          updateSetting(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    },
  },
};
</script>
